<?php if (isset($success_message)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">Guia creada con exito!</span></div>
    </div>
<?php endif; ?>
<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_formAddPurchase" style="cursor: pointer; left: 559.5px;">
    <div class="noty_message"><span id="contentMessageErrorFormAddPurchase" class="noty_text">Debe agregar algún producto</span></div>
</div>
<?php if (isset($success_message2)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">Guia actualizada con exito!</span></div>
    </div>
<?php endif; ?>



<script type="text/javascript">
    $(document).ready(function() {
        
        

<?php if (isset($searchsales)) {
    
} else { ?>
           
         $('.eliminar_1').live('click',function(){
              var numero = $(this).attr('numero_fila');
              var subtotal_fila = $('input[name=total_subtotalprice_'+numero+']').val();
              var subtotal_detalle_de_guia = $('input[name=subtotalgeneral]').val();
              var subtotal_restado = subtotal_detalle_de_guia - subtotal_fila;
              var subtotal_restadofinal = (subtotal_restado).toFixed(2);
              $('input[name=subtotalgeneral]').val(subtotal_restadofinal);
              $('.fila_detalle_'+numero+'').remove();
         });
//          
         $.ajax({
                    'url': '<?php echo base_url(); ?>index.php/shop/codeguia',
                    'success': function(res) {
                        $('input[name=numero]').val('');
                        $('input[name=numero]').val(res);
                        $('#numero').css('background', '#ffffff');
                        
                    }
                });
            
           
               
           

<?php } ?>

        $('#noty_success_1355937644582').click(function() {
            $('#noty_success_1355937644582').fadeOut('fast');
        });

        
       

    })
</script>
<div class="grid_container">
    <form id="formAddPurchase" action="
     <?php
    if (isset($searchsales)) {
        echo base_url() . 'index.php/shop/search';
    } else {
        echo base_url() . 'index.php/shop/submit';
    }
    ?> " method="post" class="form_container">
        <div class="grid_12">
            <div class="widget_wrap">
                <div class="widget_top">
                    <span class="h_icon magnifying_glass"></span>
                    <h6>
<!-- <h6><?/* $estado = isset($supplier->id_supplier)? 'Editar' : 'Agregar' ?>Nueva Guia de Compra<a class="action-icons c-add" href="<?php echo base_url(); ?>index.php/shop/add/" original-title="Nuevo">Nuevo</a></h6>
                                <input name="estado" id="estado" type="hidden" value="<? echo $estado*/?>"> -->

                    
<?php if (isset($searchsales)) {
    if (isset($datesale)) {
       echo 'Editar Guía de Compra'; ?>  
    <?php }else
    {
        echo 'Buscar Guía de Compra'; ?>  
  <?php } ?>
    
                          
<?php } else {
    echo 'Nueva Guía de Compra';?>                                                        
<?php } ?> 
                    </h6>
                </div>

                <div class="widget_content" style="margin-bottom: 30px">
                    <ul>
                        <li>
                            
                                  
                                
                                     
                                        <span class="field_title">
                                           </span>
                                       

                            <div class="form_grid_12">
                                <div class="form_input">
                                    <div class=" form_grid_4 alpha">
                                    <label class="field_title">   N° Documento</label>

<input type="text" name="numero" id="numero"  class="noEmpty" value="<?php if (isset($datesale)) {
                                        foreach ($datesale as $da) {
                                            echo $da->document_code;
                                        }
                                    } ?>"  <?php
                                        
                                       
                                         if(isset($datesale)){
                                             echo "";
                                         }else{
                                             echo "";                                             
                                         }
                                           
                                        ?>
                                    tabindex="2">


                                      
                                    </div>


                               <div><span></span></div>
                               <div id="fecha">
                                 <div class=" form_grid_4 alpha">

<label class="field_title">Fecha  </label>
                                        <input type="text" name="date"  id="numero" value="<?php
                                        if (isset($datesale)) {
                                            foreach ($datesale as $da) {
                                                $datei = $da->date;
                                                $date = str_replace('-', '/', $datei);
                                                $datef = date("d/m/Y", strtotime($date) );
                                                echo $datef;                                                
                                            }
                                        } else {
                                            if (isset($parametro))
                                            {
                                                echo "";
                                            }
                                            else
                                            {
                                                  echo date('d/m/Y');
                                            }
                                          
                                        }
                                        ?>" tabindex="3">
                               

                                </div>
                                </div> <span class="clear"></span>
                            </div>
                        </div>
                    </li>
                    <li>
                                     <div class="form_grid_12">
                                <div class="form_input">
                                       <div class="form_grid_5 alpha">

  <label class="field_title" for="type_document">Razón Social </label>
                                         <?php
                                        $options = array();
                                        $options[0] = "";
                                        foreach ($company as $td) {
                                            $options[$td->id_company] = $td->name;
                                        }

                                        $extra = 'id="company" tabindex="3" style="width:340px" class="chzn-select full" data-placeholder="Elegir..."';

                                        $buscando_almacen_de_guia = ''; /* variable que contendra el almacen de la guia buscada */
                                        if (isset($datesale)) {
                                            foreach ($datesale as $da) {
                                                $buscando_almacen_de_guia = $da->id_supplier;
                                            }
                                        }

                                        echo form_dropdown('supplier', $options, $buscando_almacen_de_guia, $extra);
                                        ?>
                                        

     
                              
                                </div>
                                <span class="clear"></span> 
                                </div>
                            </div>
                     
                         

                            </li>
                            <li>
                            <div class="form_grid_12">
                                <div class="form_input">
                                   <div class="form_grid_8 alpha">
                                        <label class="field_title">Observaciones</label>
                                        <input type="text" name="observations" id="observations" tabindex="5" value="<?php if (isset($datesale)) {
    foreach ($datesale as $da) {
        echo $da->observations;
    }
} else {
    
} ?>">
                                    </div>
                                    <span class="clear"></span>
                                </div>
                            </div>
                        </li>
                    </ul>
<?php if (isset($searchsales)) {                                         
  
    if (isset($datesale)) {
        
        if(count($datesale)== 0)           
        {    
        ?> <div class="form_grid_2" style="margin-bottom: 10px; float: right; margin-right: 0px;">
                        <button class="btn_small btn_blue"><span>Buscar</span></button>
                        </div><?php                    
        ?>
        <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644583" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">No se encontraron registros</span></div>
        </div>   
        <?
        }                   
        foreach ($datesale as $da) {
            $valor_guia = $da->document_code;
                                     
            if ($valor_guia != "") {
                ?>
                                    <script>
                                        $(document).ready(function() {
                                            $('#addProduct').click(function(e) {
                                                $.ajax({
                                                    'url': url + '/shop/products/',
                                                    'type': 'POST',
                                                    'data': {
                                                        'code': '',
                                                        'description': ''
                                                    },
                                                    'success': function(res) {
                                                        $('#add-product-model').html(res);
                                                    }
                                                });
                                                $('#add-product-model').modal();
                                                return false;
                                            });

                                        })
                                    </script>

                                    <div class="form_grid_2 " style="margin-bottom: 10px; float: right">
                                        <button id="addProduct" class="btn_small btn_blue"><span>Agregar Producto</span></button>
                                    </div>
                                    
                <?php
       
            } 
                 
        }

    }         
        else {?> <div class="form_grid_2" style="margin-bottom: 10px; float: right; margin-right: 0px;">
                        <button class="btn_small btn_blue"><span>Buscar</span></button>
                        </div><?php 
                               
    }

    ?>
                        

                            <?php } else { ?>
                    
                    <script>
                    $(document).ready(function(){
                        $('#add_products').click(function(e) {
                                $.ajax({
                                    'url' : url+'/shop/products/',
                                    'type' : 'POST',
                                    'data' : {
                                        'code' : '',
                                        'description' : ''
                                    },
                                    'success' : function(res){
                                        $('#add-product-model').html(res);
                                        }
                                    });
                                        $('#add-product-model').modal();
                                        return false;
                                });
    
                    })
                    </script>
                            
                    
                        <div class="form_grid_2 " style="margin-bottom: 10px; float: right">
                            <button id="add_products"  class="btn_small btn_blue"><span>Agregar Producto</span></button>
                        </div>

<?php } ?>

                    
                    <table class="wtbl_list">
                        <thead>
                            <tr class="tr_1">
                                <th width="10%" class="hola">Código</th>
                                <th width="30%">Descripción</th>
                                <th width="10%">Cajas</th>
                                <th width="10%">Empaques</th>
                                <th width="10%">Unidades</th>
                                <th width="10%">Precio Empaque</th>
                                <th width="10%">Total</th>
                                <th width="10%"></th>
                            </tr>

                        </thead>

                        <tbody id="contentProductList" class="contentProductList">
<?php
if (isset($datesale)) {
    $contandoitem = 0;
   
    foreach ($saledetails as $sa) {                            
        $contandoitem++;       
        ?>
                                <script>
                                    $(document).ready(function() {

                                        $('#contenaumentador').val(<?php echo $contandoitem; ?>);
                                        

                                        $('.eliminar_<?php echo $contandoitem; ?>').click(function() {

                                            if (confirm('¿Desea eliminar este producto?')) {

                                                var subtotal_2 = $('input[name=subtotal_<?php echo $contandoitem; ?>_]').val();
                                                var subtotalgeneral = $('input[name=subtotalgeneral]').val();
                                                var total = subtotalgeneral - subtotal_2;
                                                var totaldecimal = (total).toFixed(2);
                                                $('input[name=subtotalgeneral]').val(totaldecimal);


                                                var contandoitem = $('input[name=contandoitem]').val();



                                                $('input[name=contandoitem]').val(contandoitem - 1);

                                                $.ajax({
                                                    'url': url + '/sales/delete_detail',
                                                    'type': 'POST',
                                                    'data': {
                                                        'numero': $('input[name=numero]').val(),
                                                        'code': $('input[name=codigo_<?php echo $contandoitem; ?>_]').val()
                                                    },
                                                    'success': function(res) {
                                                        if (res == 1) {

                                                            $.ajax({
                                                                'url': url + '/sales/edit_amout',
                                                                'type': 'POST',
                                                                'data': {
                                                                    'numero': $('input[name=numero]').val(),
                                                                    'amount': $('input[name=subtotalgeneral]').val()
                                                                },
                                                                'success': function(res) {

                                                                }
                                                            });
                                                        } else {
                                                            alert('No se pudo eliminar el producto.');
                                                        }
                                                    }
                                                });

                                                $('tr.fila_<?php echo $contandoitem; ?>').remove();

                                            } else {

                                            }
                                        })


                                    })
                                </script>  
                                <tr class="fila_detalle_<?php echo $contandoitem; ?>">
                                    <td align="center">
                                        <input type="hidden" name="code_product_a<?php echo $contandoitem; ?>" value="<?php echo $sa->code; ?>">
                                        <?php echo $sa->code; ?>
                                    </td>
                                    <td align="center">
                                        <input type="hidden" name="description_a<?php echo $contandoitem; ?>" value="<?php echo $sa->description; ?>">
                                <?php echo $sa->description; ?>
                                    </td>
                                    <td align="center">
                                        <input type="hidden" name="total_cajas_a<?php echo $contandoitem; ?>" value="<?php echo $sa->box; ?>">
                                <?php echo $sa->box; ?></td>
                                    <td align="center">
                                        <input type="hidden" name="total_paquetes_a<?php echo $contandoitem; ?>" value="<?php echo $sa->pack; ?>">
        <?php echo $sa->pack; ?></td>
                                    <td align="center"><input type="hidden" name="total_unidades_a<?php echo $contandoitem; ?>" value="<?php echo $sa->unit; ?>">
        <?php echo $sa->unit; ?></td>
                                    <td align="center"><input type="hidden" name="total_price_a<?php echo $contandoitem; ?>" value="<?php echo $sa->price; ?>">
                            <?php echo $sa->price; ?></td>
                                    <td align="center"><input type="hidden" name="total_subtotalprice_a<?php echo $contandoitem; ?>" value="<?php echo $sa->amount; ?>">
        <?php echo "S/. ".$sa->amount; ?></td>
                                    <td align="center"><a href="#"  class="action-icons c-Delete eliminar_<?php echo $contandoitem; ?>">Delete</a></td>
                                </tr>
                        <?php
                        }
                    } else {
                        
                    }
                    ?>                            


                    <input type="hidden" name="contador_detalles" id="contador_detalles" value="0" >
                        </tbody>



                    </table>
                   <input type="hidden" name="contandoitem" value="<?php
                           if (isset($contandoitem)) {
                               echo $contandoitem;
                           }
                    ?>">

                    <input type="text" name="subtotalgeneral" id="subtotalgeneral" placeholder="0.00" value="<?php
                           if (isset($datesale)) {
                               foreach ($datesale as $da) {
                                   echo $da->amount;
                               }
                           } else {
                               echo "0.00";
                           }
                    ?>" readonly="true" style=" text-align:center; width: 100px; margin-right: 58px; float: right; background: #D0D0D0; font-family: arial; font-size: 15px; font-weight: bold;">
                    <input type="hidden" name="contenaumentador" id="contenaumentador" value="0">
                    <script type="text/javascript" >

                        /*var subtotal = 0;
                         function sumando(dato) {
                         var valor_catual =$('#subtotalgeneral').val();
                         //subtotal = parseFloat(valor_catual);
                         subtotal += dato;
                         subtotal_general_actual =  $('#subtotalgeneral').val(subtotal).toFixed(2);
                         parseFloat(subtotal_general_actual).toFixed(2);
                         }*/

                        function probar(numero) {
                             
                            $('tr.fila_detalle_' + numero + '').remove();
                            
                        }

                        $('button.c-Delete').live('click', function() {
                            if (confirm('¿Desea eliminar este producto?')) {
                            var a = $(this).val();
                            var valor_total_producto = $('input[name=total_subtotalprice_' + a + ']').val();
                            var valor_subtotal = $('input[name=subtotalgeneral]').val();

                            var subtotal_catual = (parseFloat(valor_subtotal) - parseFloat(valor_total_producto)).toFixed(2);

                            $('input[name=subtotalgeneral]').val(subtotal_catual);

                            subtotal = parseFloat(subtotal_catual);
                            
                            var contador_actual = $('#contador_detalles').val();
                            var contador_despues = contador_actual - 1;
                            contandoitem(contador_despues);
                            
                            probar(a);
                            
                            }
                            else
                                {
                                    
                                }
                        });


                        var contando = 0;

                        function contandoitem(dato) {
                            contando = dato;
                            $('#contador_detalles').val(contando);
                        }


                    </script>




                </div>
            </div>
            <br />
<?php if (!isset($parametro) == 1) : ?>
                <script type="text/javascript">
                    $(document).ready(function() {

                        function checkCode() {

                            $.ajax({
                                'url': url + '/shop/search_code_unique',
                                'type': 'POST',
                                'data': {'numero': $('input[name=numero]').val()},
                                'success': function(res) {
                                  //  alert("alert res: " + res);
                                        if (res == 1) {

                                       

                                        alert('El codigo del documento ya fue ingresado anteriormente Sistema!!');
                                        $('#numero').css('background', '#ff3333');
                                        

                                    } else if (res == 0) {

                                       
                                        $('#numero').css('background', '#ffffff');
                                        $('#formAddPurchase').submit();
                                    }
                                }
                            });

                        }

                        $('#numero').focus(function() {
                            $('#numero').css('background', '#ffffff');
                        })
                        $('input[name=date]').focus(function() {
                            $('input[name=date]').css('background', '#ffffff');
                        })


                        function formtrue() {
                            $('formAddPurchase').submit(function() {
                                return true;
                            });
                        }
                        function formfalse() {
                            $('formAddPurchase').submit(function() {
                                return false;
                            });
                        }

                        $('#guardar_guia').click(function() {
                            if ($('#contador_detalles').val() >= 1 && $('#numero').val()!='') {

                                checkCode();

                            } else {
                                
                                $('div[id=noty_success_1355937644582]').css('display', 'none');
                                $('div[id=message_error_formAddPurchase]').css('display', 'block');

                                if ($('#numero').val() == '') {
                                    $('#numero').css('background', '#ff3333');
                                }

                                var fechaactual = new Date();


                                if ($('input[name=date]').val() == '') {
                                    $('input[name=date]').css('background', '#ff3333');
                                }





                            }

                        });
                    })

  <?php endif; ?>

                </script>

 <?php
                if (isset($searchsales)) {
                    
                     if (isset($datesale)) {
               
                            foreach ($datesale as $da) {
                             $da->id_shop;
                ?>
                <input type="hidden" name="id_shop" id="id_shop" value="<? echo $da->id_shop;?>">
                <?
                              
                ?>

                        <script>
                            $(document).ready(function() {
                                $('#Editar_guia_compra').click(function() {
                                    $('#formAddPurchase').attr('action', '<?php echo base_url(); ?>index.php/shop/submit');
                                    $('#formAddPurchase').submit();
                                })
                            })
                        </script>
                        
                        <button type="button" id="Editar_guia_compra" class="btn_small btn_orange"><span>&nbsp;&nbsp;&nbsp;Guardar&nbsp;&nbsp;&nbsp;</span></button> 
                        <button type="button" class="btn_small btn_orange"><span><a id="buscar_guia" href="<?php echo base_url(); ?>index.php/shop/">Cancelar</a></span></button>
                                      
                <?php
                        }
                    }
                } else {
                ?>
                    <button type="button" id="guardar_guia" class="btn_small btn_orange"><span>Guardar</span></button>
                                   
                                <?php } ?>
                       

                <!-- 
   <div class="form_grid_12">
                <button type="button" id="guardar_guia" class="btn_small btn_orange"><span>Guardar</span></button> -->

                          
        </div>
    </form>
    <span class="clear"></span>

    <div id="add-product-model">
    </div>
</div>

<table id="listProductBlank" class="clear">
    <tr id="rowListProductBlank" class="tr_even">
        <td class="elementList codeProduct"></td>
        <td class="elementList descriptionProduct"></td>
        <td class="elementList quantityBoxesProduct"></td>
        <td class="elementList quantityPackagesProduct"></td>
        <td class="elementList quantityUnitsProduct"></td>
        <td class="elementList priceProduct"></td>
        <td class="elementList inputProduct"><a href="#" class="action-icons c-Delete delProductList" original-title="Borrar">Delete</a></td>
    </tr>
</table>